/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.sys.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.apestar.common.persistence.Page;
import cn.apestar.common.service.CrudService;
import cn.apestar.modules.cache.RemoveCacheUtil;
import cn.apestar.modules.sys.component.InstitutionDataCovert;
import cn.apestar.modules.sys.dao.InstitutionDao;
import cn.apestar.modules.sys.entity.Institution;

/**
 * 机构表Service
 * @author LVH
 * @version 2016-02-21
 */
@Service
@Transactional(readOnly = true)
public class InstitutionService extends CrudService<InstitutionDao, Institution> {

    /**
     * 持久层对象
     */
    @Autowired
    protected InstitutionDao dao;

    @Override
    public Institution get(String id) {

        return InstitutionDataCovert.translateInstitution(super.get(id));
    }

    @Override
    public List<Institution> findList(Institution institution) {

        return InstitutionDataCovert.translateListInstitution(super.findList(institution));
    }

    @Override
    public Page<Institution> findPage(Page<Institution> page, Institution institution) {

        page = super.findPage(page, institution);

        for (Institution is : page.getList()) {

            List<String> st2 = new ArrayList<String>();

            if ("0".equals(is.getDelFlag())) {
                is.setState("停用");
            }

            if ("1".equals(is.getDelFlag())) {
                is.setState("正常");
            }

            if (is.getOnBusinessWeekday() == null) {
                st2.add("未填写");
            } else {

                String day = is.getOnBusinessWeekday();

                String[] days = day.split(",");

                for (int i = 0; i < days.length; i++) {

                    if ("0".equals(days[i])) {
                        st2.add("周天 ");
                    } else if (days[i].trim() != null) {
                        Integer j = Integer.parseInt(days[i]);

                        st2.add(" 周" + j);
                    }

                }
            }

            is.setOnBusinessWeekdayList(st2);

        }

        InstitutionDataCovert.translateListInstitution(page.getList());
        return page;
    }

    @Transactional(readOnly = false)
    public void updateState(Institution institution) {
        if (!StringUtils.isEmpty(institution.getId())) {
            RemoveCacheUtil.removeInstitutionCache(institution.getId());
        }
        dao.updateState(institution);
    }

    @Transactional(readOnly = false)
    public void updateDel(Institution institution) {
        if (!StringUtils.isEmpty(institution.getId())) {
            RemoveCacheUtil.removeInstitutionCache(institution.getId());
        }
        dao.updateDel(institution);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(Institution institution) {
        super.save(institution);
        if (!StringUtils.isEmpty(institution.getId())) {
            RemoveCacheUtil.removeInstitutionCache(institution.getId());
        }

    }

    @Override
    @Transactional(readOnly = false)
    public void delete(Institution institution) {

        super.delete(institution);
    }

    public Page<Institution> findByProxyTypeList(Page<Institution> page, Institution institution) {

        page = this.findProxyPage(page, institution);
        InstitutionDataCovert.translateListInstitution(page.getList());
        return page;
    }

    /**
     * 查询分页数据
     * @param page 分页对象
     * @param entity
     * @return
     */
    private Page<Institution> findProxyPage(Page<Institution> page, Institution entity) {
        entity.setPage(page);
        page.setList(dao.findByProxyTypeList(entity));
        return page;
    }
}